﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AuthorizeList.ascx.cs" company="VDC-IT" author="ltduc">
//   AuthorizeList codebehind
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace VDC.VONE.WEB.Modules.Authorize.Controls
{
    #region

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Reflection;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using AdCom.Utility;
    using VDC.COMMON;
    using VDC.COMMON.ExceptionHelpers;
    using VDC.VONE.BLL;
    using VDC.VONE.ENTITIES;

    #endregion
    /// <summary>
    /// Class AuthorizeList
    /// </summary>
    public partial class AuthorizeList : System.Web.UI.UserControl
    {
        #region Constants

        /// <summary>
        ///     Url AuthorizeEdit
        /// </summary>
        private const string UrlAuthorizeAdd = "AuthorizeEdit.aspx?PageIndex={0}";

        /// <summary>
        ///     Url Edit Authorize
        /// </summary>
        private const string UrlAuthorizeEdit = "AuthorizeEdit.aspx?Id={0}&PageIndex={1}";

        /// <summary>
        /// The edit
        /// </summary>
        private const string Edit = "Edit$";

        /// <summary>
        /// The zero
        /// </summary>
        private const string Zero = "0";

        /// <summary>
        /// The Authorize id
        /// </summary>
        private const string AuthorizeId = "AuthorizeId";

        /// <summary>
        /// Index trang
        /// </summary>
        private readonly int pageIndex = HtmlHelper.GetIntFromRequestString(Constants.PageIndex, 0);
        #endregion

        #region Fields

        /// <summary>
        ///     The mouse out
        /// </summary>
        private readonly string mouseOut =
            "this.style.backgroundColor=self.MouseOverOldColor; this.style.color='Black';";

        /// <summary>
        ///     The mouse over
        /// </summary>
        private readonly string mouseOver =
            "self.MouseOverOldColor=this.style.backgroundColor;this.style.color='#C04000';";

        #endregion

        #region Methods

        /// <summary>
        /// The grd Authorize_ data bound.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void GrdAuthorize_DataBound(object sender, EventArgs e)
        {
            PublicFuncs.ApplyPaging(this.GrdAuthorize);
        }

        /// <summary>
        /// Sự kiện PageIndexChanging
        /// </summary>
        /// <param name="sender">
        /// The sender-page
        /// </param>
        /// <param name="e">
        /// The e
        /// </param>
        protected void GrdAuthorize_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                this.GrdAuthorize.PageIndex = e.NewPageIndex;
                this.BindData();
                this.lblMessage.Text = string.Empty;
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                ExceptionHandler.Instance.HandleUiException(ex);
            }
        }

        /// <summary>
        /// Xóa ủy quyền đang chọn trên grid
        /// </summary>
        /// <param name="sender">
        /// The sender-grid
        /// </param>
        /// <param name="e">
        /// The e - event deleting
        /// </param>
        protected void GrdAuthorize_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                AuthorizeService authorizeService = new AuthorizeService();
                var key = this.GrdAuthorize.DataKeys[e.RowIndex];
                if (key != null)
                {
                    int id = int.Parse(key.Value.ToString());
                    authorizeService.Delete(id);
                }

                this.BindData();
                this.lblMessage.Text = PublicFuncs.GetMessage(Messages.MessageDeleteAuthorizeSuccess);
                LogHelper.Instance.WriteLog(HttpContext.Current.User.Identity.Name, methodName, false);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                ExceptionHandler.Instance.HandleUiException(ex);
            }
        }

        /// <summary>
        /// Sự kiện row edit
        /// </summary>
        /// <param name="sender">
        /// The sender
        /// </param>
        /// <param name="e">
        /// The e
        /// </param>
        protected void GrdAuthorize_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                object id = -1;
                id = this.GrdAuthorize.DataKeys[e.NewEditIndex].Values[AuthorizeId];
                this.Response.Redirect(string.Format(UrlAuthorizeEdit, id, this.GrdAuthorize.PageIndex), false);
                LogHelper.Instance.WriteLog(HttpContext.Current.User.Identity.Name, methodName, false);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                ExceptionHandler.Instance.HandleUiException(ex);
            }
        }

        /// <summary>
        /// Sự kiện DataBound của row
        /// </summary>
        /// <param name="sender">
        /// The sender - grid
        /// </param>
        /// <param name="e">
        /// The e
        /// </param>
        protected void GrdAuthorize_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;

            ////Ghi Log
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes[Constants.ClientEvent.MouseOver] = this.mouseOver;
                    e.Row.Attributes[Constants.ClientEvent.MouseOut] = this.mouseOut;
                    for (int i = 1; i < this.GrdAuthorize.Columns.Count - 1; i++)
                    {
                        e.Row.Cells[i].Attributes.Add(Constants.ClientEvent.Click, this.Page.ClientScript.GetPostBackEventReference((GridView)sender, Edit + e.Row.RowIndex.ToString(CultureInfo.CurrentCulture)));
                    }
                }

                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                ExceptionHandler.Instance.HandleUiException(ex);
            }
        }

        /// <summary>
        /// Click thêm mới
        /// </summary>
        /// <param name="sender">
        /// The sender - Button
        /// </param>
        /// <param name="e">
        /// The e
        /// </param>
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;

            ////Ghi Log
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                string currentUrl = this.Request.AppRelativeCurrentExecutionFilePath;
                this.Response.Redirect(string.Format(UrlAuthorizeAdd, this.GrdAuthorize.PageIndex), false);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                ExceptionHandler.Instance.HandleUiException(ex);
            }
        }

        /// <summary>
        /// Phương thức Init của trang
        /// </summary>
        /// <param name="sender">
        /// The sender
        /// </param>
        /// <param name="e">
        /// The e
        /// </param>
        protected void Page_Init(object sender, EventArgs e)
        {
            this.GrdAuthorize.PageSize = PublicFuncs.PageSize;
        }

        /// <summary>
        /// Phương thức Load của trang
        /// </summary>
        /// <param name="sender">
        /// The sender
        /// </param>
        /// <param name="e">
        /// The e
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {
            ////Thực hiện ghi log
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                if (!this.IsPostBack)
                {
                    this.GrdAuthorize.PageIndex = this.pageIndex;
                    this.BindData();
                }
                else
                {
                    PublicFuncs.ApplyPaging(this.GrdAuthorize);
                }

                LogHelper.Instance.WriteLog(HttpContext.Current.User.Identity.Name, methodName, false);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                ExceptionHandler.Instance.HandleUiException(ex);
            }
        }

        /// <summary>
        ///     Binding data to grid
        /// </summary>
        private void BindData()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var user = new UserEN();
                var userService = new UserService();
                user = userService.GetUserByUserName(this.Page.User.Identity.Name);
                IList<AuthorizeEN> lstAuthorize = new List<AuthorizeEN>();
                AuthorizeService authorizeService = new AuthorizeService();
                lstAuthorize = authorizeService.GetAuthorizeByAuthorizeUserId(user.UserId);
                this.lblCount.Text = lstAuthorize.Count.ToString();
                this.GrdAuthorize.DataSource = lstAuthorize;
                this.GrdAuthorize.DataBind();
                LogHelper.Instance.WriteLog(HttpContext.Current.User.Identity.Name, methodName, false);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                ExceptionHandler.Instance.HandleUiException(ex);
            }
        }
        #endregion
    }
}